package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.backlist.CardBlackListUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.backlist.CardBlackListUploadResponse;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.CardBlackListUploadDao;
import io.renren.modules.basedata.entity.Tbl_cardBlackList;
import io.renren.modules.basedata.service.CardBlackListUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class CardBlackListUploadServiceImpl implements CardBlackListUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private CardBlackListUploadDao cardBlackListUploadDao;

    @Override
    public String cardBlackListUpload() throws Exception {
        log.info("开始获取未上传客户卡黑名单");
        List<Tbl_cardBlackList> list = getCardBlacklistList();//获取未上传客户卡黑名单信息
        Tbl_cardBlackList cardBlack=null;
        if(list!=null&&list.size()>0){

            for(int i=0;i<list.size();i++){

                cardBlack = list.get(i);

                CardBlackListUploadRequest localCardBlackListUploadRequest = new CardBlackListUploadRequest();

                localCardBlackListUploadRequest.setCardId(cardBlack.getCardId().trim());

                localCardBlackListUploadRequest.setCreationTime(DateUtil.formatDateTimes(cardBlack.getCreationTime()));

                localCardBlackListUploadRequest.setIssuerId(cardBlack.getIssuerId().trim());

                localCardBlackListUploadRequest.setStatus(cardBlack.getStatus());

                localCardBlackListUploadRequest.setType(cardBlack.getType());
                //获取发送文件名
                String fileName = ETCCommHelper.getFileName(localCardBlackListUploadRequest,"65");
                //生成就json文件
//				CreateFileUtil.createJsonFile(localCardBlackListUploadRequest.toJsonStr(), "",fileName);

                try{
                    //指定响应的协议模型,调用upload发送数据
                    CardBlackListUploadResponse response = ETCCommHelper.sdtsUpload(fileName, localCardBlackListUploadRequest, CardBlackListUploadResponse.class);

                    log.info("上传用户卡黑名单信息服务器返回："+response.toJson());

                    if(response!=null&&response.getInfo().equals("成功！")){

                        //上传成功,更新客户信息表状态
                        if(localCardBlackListUploadRequest.getStatus()==1){//加入黑名单操作,更新状态
                            if(cardBlackListUploadDao.updateCardBlackListUploadflag(cardBlack)==1){//删除数据
                                log.info("更新用户卡黑名单信息数据成功");

                            }else{
                                log.info("更新用户卡黑名单信息数据失败");
                            }

                        }else{

                            if(cardBlackListUploadDao.deleteUploadcardBlack(cardBlack)==1){//删除数据

                                log.info("删除用户卡黑名单信息数据成功");
                            }else{

                                log.info("删除用户卡黑名单信息数据失败");
                            }
                        }

                    }else{

                        log.info("上传用户卡黑名单信息失败,失败信息："+response.getInfo());

                        cardBlackListUploadDao.updateCardBlackListUploadflagError(cardBlack);
                    }

                }catch(IOException e){

                    log.error("上传用户卡黑名单信息失败！失败信息:", e);
                    e.printStackTrace();
                    continue;

                }catch (ApiRequestException apie) {

                    log.error("上传用户卡黑名单信息失败！失败信息:", apie);

                    if(apie.getErrCode()>=700&&apie.getErrCode()<=800){
                        if(apie.getErrCode()==707){
                            if(cardBlack.getStatus()==1){
                                cardBlackListUploadDao.updateCardBlackListUploadflag(cardBlack);
                            }else{
                               cardBlackListUploadDao.deleteUploadcardBlack(cardBlack);
                            }
                        }else{
                            cardBlackListUploadDao.updateCardBlackListUploadflagError(cardBlack);
                        }
                    }
                    apie.printStackTrace();

                    continue;
                }
            }
            return "用户卡黑名单信息上传结束...";
        }else{

            return "无用户卡黑名单信息,上传结束...";
        }
    }
    //获取充值信息
    @SuppressWarnings("unchecked")
    public List<Tbl_cardBlackList> getCardBlacklistList() throws Exception {
        List<Tbl_cardBlackList> list;
        try {
            list = cardBlackListUploadDao.getCardBlackList();
        } catch (Exception e1) {
            e1.printStackTrace();
            log.error("获取未上传卡黑名单信息失败!错误信息：", e1);
            throw new Exception("获取未上传卡黑名单信息失败："+e1.getMessage());
        }
        return list;
    }
}
